﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using EnvDTE;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using ProjectClone.CloneHandle;
using ProjectClone.Dialog;
using ProjectClone.Tool;

namespace ProjectClone
{
    /// <summary>
    /// Command handler
    /// </summary>
    internal sealed class CommandToClone
    {
        /// <summary>
        /// Command ID.
        /// </summary>
        public const int CommandId = 0x0100;

        /// <summary>
        /// Command menu group (command set GUID).
        /// </summary>
        public static readonly Guid CommandSet = new Guid("b837ad55-82c4-4057-8e87-10a9d3b96896");

        /// <summary>
        /// VS Package that provides this command, not null.
        /// </summary>
        private readonly Package package;

        /// <summary>
        /// Initializes a new instance of the <see cref="CommandToClone"/> class.
        /// Adds our command handlers for menu (commands must exist in the command table file)
        /// </summary>
        /// <param name="package">Owner package, not null.</param>
        private CommandToClone(Package package)
        {
            if (package == null)
            {
                throw new ArgumentNullException("package");
            }

            this.package = package;

            OleMenuCommandService commandService = this.ServiceProvider.GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
            if (commandService != null)
            {
                var menuCommandID = new CommandID(CommandSet, CommandId);
                var menuItem = new MenuCommand(this.MenuItemCallback, menuCommandID);
                commandService.AddCommand(menuItem);
            }
        }

        /// <summary>
        /// Gets the instance of the command.
        /// </summary>
        public static CommandToClone Instance
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the service provider from the owner package.
        /// </summary>
        private IServiceProvider ServiceProvider
        {
            get
            {
                return this.package;
            }
        }

        /// <summary>
        /// Initializes the singleton instance of the command.
        /// </summary>
        /// <param name="package">Owner package, not null.</param>
        public static void Initialize(Package package)
        {
            Instance = new CommandToClone(package);
        }

        /// <summary>
        /// This function is the callback used to execute the command when the menu item is clicked.
        /// See the constructor to see how the menu item is associated with this function using
        /// OleMenuCommandService service and MenuCommand class.
        /// </summary>
        /// <param name="sender">Event sender.</param>
        /// <param name="e">Event args.</param>
        private void MenuItemCallback(object sender, EventArgs e)
        {
            try
            {
                IVsSolution pSolution = this.ServiceProvider.GetService(typeof(SVsSolution)) as IVsSolution;
                object isSolutionOpen = null;
                pSolution.GetProperty((int)__VSPROPID.VSPROPID_IsSolutionOpen, out isSolutionOpen);

                if (Convert.ToBoolean(isSolutionOpen))
                {
                    throw new BizException("请关闭当前解决方案后进行操作");
                }

                Window1 projectDialog = new Window1();
                if (projectDialog.ShowDialog() ?? false == true)
                {
                    EnvDTE80.DTE2 _dte2 = (EnvDTE80.DTE2)ServiceProvider.GetService(typeof(EnvDTE.DTE));

                    var cloneHandler = new ProjectCloneHandler(new ProjectCloneOption
                    {
                        ServiceProvider = this.ServiceProvider,
                        Solution = pSolution,
                        DTE2 = _dte2,
                        SourceFullName = projectDialog.SourceFullName, //@"E:\JwellDocment\ECommerce\MSAServices\Jwell.ECommerce.CMS\Jwell.ECommerce.CMS.sln",
                        DestinationDir = projectDialog.DestinationDirPath + "\\" + projectDialog.DestinationName + "\\", //"E:\\ProjectCloneTest\\CloneTest\\",
                        DestinationName = projectDialog.DestinationName //"Jwell.ECommerce.Template"
                    });

                    cloneHandler.Excute();
                }
            }
            catch (BizException ex) {
                VsShellUtilities.ShowMessageBox(
                    this.ServiceProvider,
                    ex.Message,
                    "",
                    OLEMSGICON.OLEMSGICON_INFO,
                    OLEMSGBUTTON.OLEMSGBUTTON_OK,
                    OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
            }
        }
    }
}
